package com.xuan.mail.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.mail.domin.SpamKeyword;
import com.xuan.mail.service.SpamKeywordService;
import com.xuan.mail.mapper.SpamKeywordMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* @author 86156
* @description 针对表【spam_keyword】的数据库操作Service实现
* @createDate 2024-04-11 10:03:22
*/
@Service
public class SpamKeywordServiceImpl extends ServiceImpl<SpamKeywordMapper, SpamKeyword>
    implements SpamKeywordService{
    @Autowired
    private SpamKeywordMapper spamKeywordMapper;

    // 获取所有垃圾邮件关键词
    @Override
    public List<String> getAllSpamKeywords() {
        return spamKeywordMapper.selectList(null).stream()
                .map(SpamKeyword::getSpamKeyword)
                .collect(Collectors.toList());
    }

    // 检测邮件是否为垃圾邮件
    @Override
    public boolean isSpamEmail(String emailContent, List<String> spamKeywords) {
        Pattern pattern = Pattern.compile(String.join("|", spamKeywords), Pattern.CASE_INSENSITIVE);
        return pattern.matcher(emailContent).find();
    }

}




